package com.atguigu.day03;

import com.atguigu.beans.WaterSensor;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author Felix
 * @date 2024/4/1
 * 该案例演示了转换算子-filter
 * 需求：将数据流中传感器id为ws1的数据过滤出来
 */
public class Flink10_Trans_Filter {
    public static void main(String[] args) throws Exception {
        //TODO 1.指定流处理环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //TODO 2.准备数据
        DataStreamSource<WaterSensor> wsDS
                = env.fromElements(new WaterSensor("ws1", 10L, 10), new WaterSensor("ws2", 20L, 20));
        //TODO 3.从流中的WaterSensor对象中过滤出传感器id为ws1的数据
        SingleOutputStreamOperator<WaterSensor> filterDS = wsDS.filter(
                new FilterFunction<WaterSensor>() {
                    @Override
                    public boolean filter(WaterSensor ws) throws Exception {
                        return "ws1".equals(ws.id);
                    }
                }
        );

        //TODO 4.打印
        filterDS.print();
        //TODO 5.提交作业
        env.execute();
    }
}
